package com.cloud.bc.controller.ajax.center;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.BcFeedbacks;
import com.cloud.bc.model.entity.BcSingles;
import com.cloud.bc.model.entity.BcUsers;
import com.cloud.bc.model.enums.RedisKey;
import com.cloud.bc.model.request.UpdateEmailRequest;
import com.cloud.bc.model.request.UpdateMobileRequest;
import com.cloud.bc.model.request.UpdatePasswordRequest;
import com.cloud.bc.model.response.AjaxResp;
import com.cloud.bc.service.BcFeedbacksService;
import com.cloud.bc.service.BcUsersService;
import com.cloud.redis.config.RedisUtils;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@AllArgsConstructor
@RestController
@RequestMapping("m/ajax/center/setup")
public class Ajax_SetupController {

    private final BcUsersService bcUsersService;
    private final BcFeedbacksService bcFeedbacksService;
    private final RedisUtils redisUtils;

    @PostMapping("updatepassword")
    public AjaxResp updatepassword(HttpServletRequest request, HttpServletResponse response,
                                   UpdatePasswordRequest req) {
        AjaxResp ajaxResp = new AjaxResp();
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        BcUsers bcUsers = new BcUsers();
        bcUsers.setId(userInfo.getId());
        bcUsers.setPassword(req.getYpassword());
        int count = bcUsersService.count(bcUsers);
        if (count <= 0) {
            ajaxResp.setSuccess(false);
            ajaxResp.setCode(48584);
            ajaxResp.setMsg("原密码错误");
            return ajaxResp;
        }
        bcUsersService.updateById(bcUsers);
        return ajaxResp;
    }

    /**
     * 修改手机号
     */
    @PostMapping("updatemobile")
    public AjaxResp updatemobile(HttpServletRequest request, HttpServletResponse response,
                                 UpdateMobileRequest req) {
        AjaxResp ajaxResp = new AjaxResp();
        String smsYzm = req.getSmsyzm();
        Object o = redisUtils.get(RedisKey.bcSingleBindRedisKey + req.getMobile());
        if (!StrUtil.isEmpty(smsYzm) && !smsYzm.equals(Convert.toStr(o))) {
            ajaxResp.setSuccess(false);
            ajaxResp.setMsg("验证码错误，请确认");
            return ajaxResp;
        }

        UserInfo userInfo = bcUsersService.getUserInfo(request);
        BcUsers bcUsers = new BcUsers();
        bcUsers.setMobile(req.getMobile());
        int count = bcUsersService.count(bcUsers);
        if (count > 0) {
            ajaxResp.setSuccess(false);
            ajaxResp.setCode(48584);
            ajaxResp.setMsg("手机号已存在，不可重复绑定");
            return ajaxResp;
        }
        bcUsers.setId(userInfo.getId());
        bcUsersService.updateById(bcUsers);
        return ajaxResp;
    }

    /**
     * 修改手机号
     */
    @PostMapping("updateemail")
    public AjaxResp updateemail(HttpServletRequest request, HttpServletResponse response,
                                UpdateEmailRequest req) {
        AjaxResp ajaxResp = new AjaxResp();
        String emailYzm = req.getEmailyzm();
        Object o = redisUtils.get(RedisKey.bcSingleBindRedisKey + req.getEmail());
        if (!StrUtil.isEmpty(emailYzm) && !emailYzm.equals(Convert.toStr(o))) {
            ajaxResp.setSuccess(false);
            ajaxResp.setMsg("验证码错误，请确认");
            return ajaxResp;
        }

        UserInfo userInfo = bcUsersService.getUserInfo(request);
        BcUsers bcUsers = new BcUsers();
        bcUsers.setEmail(req.getEmail());
        int count = bcUsersService.count(bcUsers);
        if (count > 0) {
            ajaxResp.setSuccess(false);
            ajaxResp.setCode(48584);
            ajaxResp.setMsg("Email已存在，不可重复绑定");
            return ajaxResp;
        }
        bcUsers.setId(userInfo.getId());
        bcUsersService.updateById(bcUsers);
        return ajaxResp;
    }


    /**
     * 获取系统消息
     */
    @GetMapping("notice")
    public AjaxResp notice(HttpServletRequest request, HttpServletResponse response,
                           Integer notice) {
        AjaxResp ajaxResp = new AjaxResp();
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        BcUsers bcUsers = new BcUsers();
        bcUsers.setId(userInfo.getId());
        bcUsers.setNotice(notice);
        bcUsersService.updateById(bcUsers);
        return ajaxResp;
    }

    /**
     * 意见反馈
     */
    @PostMapping("feedback")
    public AjaxResp feedback(HttpServletRequest request, HttpServletResponse response,
                             BcFeedbacks req) {
        AjaxResp ajaxResp = new AjaxResp();
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        if (null != userInfo && Convert.toInt(userInfo.getId()) > 0) {
            req.setUserId(userInfo.getId());
            req.setUserName(userInfo.getNickname());
        }
        bcFeedbacksService.save(req);
        return ajaxResp;
    }

}
